package com.searching.core.index.Impl;

import com.searching.config.searching_Config;
import com.searching.core.dao.DataSourceFactory;
import com.searching.core.dao.Impl.FileIndexDaoImpl;
import com.searching.core.index.FileScan;
import com.searching.core.interceptor.FileInterceptor;
import com.searching.core.interceptor.impl.FileIndexInterceptor;
import com.searching.core.interceptor.impl.FilePrintInterceptor;
import com.searching.core.model.Thing;
import com.sun.xml.internal.bind.v2.TODO;

import javax.sql.DataSource;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class FileScanImpl implements FileScan {
    //private final DataSource dataSource;
    private LinkedList<FileInterceptor> interceptors = new LinkedList<>();
    private searching_Config config = searching_Config.getInstance();
    //public FileScanImpl(DataSource dataSource) {
    //   this.dataSource = dataSource;

    @Override
    public void index(String path) {
        File file = new File(path);
        List<File> fileList = new ArrayList<>();
        if (file.isFile()) {
            if (!config.getExcludePath().contains(file.getParent())) {
                fileList.add(file);
            }
        } else {
            if (!config.getExcludePath().contains((path))) {
                File[] files = file.listFiles();
                if (files != null) {
                    for (File f : files) {
                        index(f.getAbsolutePath());
                    }
                }
            }

        }
        /**
         * 简化判断
         */

//        if (file.isFile()) {
//            if (config.getExcludePath().contains(file.getParent())) {
//                return;
//            } else {
//                fileList.add(file);
//            }
//        }else {
//            if(config.getExcludePath().contains(path)) {
//                return;
//            }else {
//                File[] files = file.listFiles();
//                if(file != null) {
//                    for(File f : files) {
//                        index(f.getAbsolutePath());
//                    }
//                }
//            }
//        }

        //File Directory
        //递归遍历后，在这里处理
        for(FileInterceptor interceptor : this.interceptors) {
            interceptor.apply(file);
            //File -> Thing -> DAO
        }

//        文件变成thing-》写入
    }
    public void addFileInterceptor(FileInterceptor fileInterceptor) {
        this.interceptors.add(fileInterceptor);
    }

    public static void main(String[] args) {
//        DataSourceFactory.initDatabase();

        FileScanImpl scan = new FileScanImpl();
        FileInterceptor printInterceptor = new FilePrintInterceptor();

        scan.addFileInterceptor(printInterceptor);

        FileInterceptor fileIndexInterceptor =
                new FileIndexInterceptor(new FileIndexDaoImpl(DataSourceFactory.dataSource()));

        //这个方法有bug
        //TODO 这个方法有bug
//         scan.addFileInterceptor(fileIndexInterceptor);



        scan.index("D:\\");
    }
}
